package com.lgCore.server;

import com.lgCore.protoModel.ProtoMsg;
import com.lgCore.rpc.common.codec.CommonRpcDecoderHandler;
import com.lgCore.rpc.common.codec.CommonRpcEncoderHandler;
import com.lgCore.rpc.common.codec.RpcProtobufDecoder;
import com.lgCore.rpc.common.codec.RpcProtobufEncoder;
import com.lgCore.serverHandler.RpcHandler;
import com.lgCore.serverHandler.HeartBeatServerHandler;
import com.lgCore.serverHandler.LoginRequestHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;


public class NettyServerBootstrap {

	private int port;
    private SocketChannel socketChannel;
    public NettyServerBootstrap(int port) throws InterruptedException {
        this.port = port;
        bind();
    }

    public SocketChannel getSocketChannel() {
        return socketChannel;
    }

    public int getPort() {
        return port;
    }

    private void bind() throws InterruptedException {
        EventLoopGroup boss=new NioEventLoopGroup();
        EventLoopGroup worker=new NioEventLoopGroup();
        ServerBootstrap bootstrap=new ServerBootstrap();
        bootstrap.group(boss,worker);
        bootstrap.channel(NioServerSocketChannel.class);
        bootstrap.option(ChannelOption.SO_BACKLOG, 128);
        //通过NoDelay禁用Nagle,使消息立即发出去，不用等待到一定的数据量才发出去
        bootstrap.option(ChannelOption.TCP_NODELAY, true);
        //保持长连接状态
        bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
        bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel socketChannel) throws Exception {
                ChannelPipeline p = socketChannel.pipeline();

//                p.addLast(new ProtobufVarint32FrameDecoder());//添加protobuff解码器
//                p.addLast(new ProtobufDecoder(ProtoMsg.Message.getDefaultInstance()));//添加protobuff对应类解码器
//                p.addLast(new ProtobufVarint32LengthFieldPrepender());//protobuf的编码器 和上面对对应
//                p.addLast(new ProtobufEncoder());//protobuf的编码器

                p.addLast("decoder", new CommonRpcDecoderHandler());
                p.addLast("encoder", new CommonRpcEncoderHandler());

//                p.addLast("deCoder", new RpcProtobufDecoder());
//                p.addLast("enCoder", new RpcProtobufEncoder());
//                p.addLast(new RpcProtobufEncoder());
//                p.addLast(new RpcProtobufDecoder());
//
//                p.addLast(new ObjectEncoder());
//                p.addLast(newp ObjectDecoder(ClassResolvers.cacheDisabled(null)));
                //心跳
                p.addLast("heartBeat",new HeartBeatServerHandler());
                p.addLast("loginRequest",new LoginRequestHandler());
                p.addLast(new RpcHandler());
            }
        });
        ChannelFuture f= bootstrap.bind(port).sync();
        if(f.isSuccess()){
            System.out.println("server start---------------");
        }
    }
    public static void main(String []args) throws InterruptedException {
        NettyServerBootstrap bootstrap=new NettyServerBootstrap(9999);
//        while (true){
//            /*SocketChannel channel=(SocketChannel)NettyChannelMap.get("101");
//            if(channel!=null){
//                AskMsg askMsg=new AskMsg();
//                channel.writeAndFlush(askMsg);
//            }
//            TimeUnit.SECONDS.sleep(5);*/
//        	//System.out.println(NettyChannelMap.getKeys());
//        	TimeUnit.SECONDS.sleep(15);
//        }
    }
}
